<?php 
require_once(dirname(dirname(__FILE__)).'/libs/db.class.php');
class Order{
    public $db;
    function __construct()
    {
        $this->db = new DB();
    }

    function index(){
        $output=['errcode'=>0,'msg'=>'success'];
        echo json_encode($output);
    }

    // 提交订单
    function add(){
        $output=['errcode'=>0,'msg'=>'success'];
        if($_POST){
            $is_login=$this->is_login();
            
            if(!$is_login){
                $output['errcode']=1;
                $output['msg']='未登录状态, 不能进行此操作';
            }else{
                $createtime=date('Y-m-d H:i:s',time());
                $order_data['user_id']=$is_login['id'];
                $order_data['order_no']=date('YmdHis',time()).uniqid();
                $order_data['consignee']=$_POST['consignee'];
                $order_data['tel']=$_POST['tel'];
                $order_data['pay_way']=$_POST['pay_way'];
                $order_data['province']=$_POST['province'];
                $order_data['city']=$_POST['city'];
                $order_data['county']=$_POST['county'];
                $order_data['address']=$_POST['address'];
                $order_data['createtime']=$createtime;

                $order_data['amount']=0;
                $order_items=json_decode($_POST['order_items'],true);
                foreach($order_items as $key=>$item){
                    $product=$this->db->get_one("select * from products where id={$item['product_id']}");
                    // 计算订单金额
                    $order_data['amount']+=$product['price']*$item['product_num'];
                    $order_items[$key]['product_name']=$product['name'];
                    $order_items[$key]['product_price']=$product['price'];
                    $order_items[$key]['product_cover']=$product['cover'];
                    $order_items[$key]['createtime']=$createtime;
                }
                $sql="insert into orders set {$this->arr2str($order_data)}";
                $res=$this->db->query($sql);
                if($res){
                    // 写入订单商品
                    $order_id=$res;
                    foreach($order_items as $key=>$item){
                        $order_items[$key]['order_id']=$order_id;
                        $this->db->query("insert into order_items set {$this->arr2str($order_items[$key])}");
                    }
                    $output['msg']='订单提交成功';
                }else{
                    $output['errcode']=1;
                    $output['msg']='订单提交失败';
                }
            }
        }else{
            $output['errcode']=2;
            $output['msg']='参数错误';
        }
        echo json_encode($output);
    }


    // 获取订单列表
    function list(){
        $output=['errcode'=>0,'msg'=>'success']; 
        $is_login=$this->is_login();
        if(!$is_login){
            $output['errcode']=1;
            $output['msg']='未登录状态, 不能进行该操作';
        }else{
            $user_id=$is_login['id'];
            $sql="select * from orders where user_id={$user_id}";
            $list=$this->db->query($sql);
            foreach($list as $key=>$item){
                $order_items=$this->db->query("select * from order_items where order_id={$item['id']}");
                if($order_items){
                    $list[$key]=$order_items;
                }
            }
        }
        echo json_encode($output);
    }

    // 获取订单详情
    function order_detail(){
        $output=['errcode'=>0,'msg'=>'success'];
        if(isset($_GET['id'])){
            $order_id=$_GET['id'];
            $is_login=$this->is_login();
            if(!$is_login){
                $output['errcode']=1;
                $output['msg']='未登录状态, 不能进行该操作';
            }else{
                $user_id=$is_login['id'];
                $sql="select * from  orders  where id={$order_id}";
                $order=$this->db->get_one($sql);
                if($order){
                    $order_items=$this->db->query("select * from order_items where order_id={$order_id}");
                    if($order_items){
                       $order['items']=$order_items;     
                    }
                }else{
                    $output['errcode']=3;
                    $output['msg']='获取订单信息失败';
                }
            }
        }else{
            $output['errcode']=2;
            $output['msg']='参数错误';
        }
        echo json_encode($output);
    }



    // 检测用户是否登录
    private function is_login(){
        // $token=$_COOKIE['Token'];
        $token=getallheaders()['Token'];
        // 从token中解密用户信息
        $user=json_decode(base64_decode($token),true);
        $sql="select id,username,nickname,avatar,tel,email,motto from users where id={$user['id']}";
        $res=$this->db->get_one($sql);
        if(!empty($res)){
            return $res;
        }else{
            return false;
        }   
    }



    // 将数组转换成查询字符串
    private function arr2str($arr){
        $str='';
        foreach($arr as $key=>$val){
            $str.=$key.'="'.$val.'",';
        }
        return substr($str,0,-1);
    }


}
